<%--
  Created by IntelliJ IDEA.
  User: QGH
  Date: 2018/7/2
  Time: 10:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>用户管理</title>
    <%@include file="/WEB-INF/pages/common/common-ztree.jsp" %>
    <%@ include file="/WEB-INF/pages/common/meta.jsp"%>
    <script type="text/javascript" src="${ctx}/static/js/cs/user/ccUser.js"></script>
    <link rel="stylesheet" type="text/css" href="${ctx}/uce/css/yousu.css" />
    <link rel="stylesheet" type="text/css" href="${ctx}/uce/themes/icon.css"/>
    <style type="text/css">
        div#rMenu {position:absolute; visibility:hidden; top:0; background-color: #555;text-align: left;padding: 2px;}
        div#rMenu ul li{
            margin: 1px 0;
            padding: 0 5px;
            cursor: pointer;
            list-style: none outside none;
            background-color: #DFDFDF;
        }
        #dlgUserRoleRel .datagrid-cell{
            padding:0px!important
        }
    </style>
</head>

<body class="easyui-layout">
<script type="text/javascript">

    //页面加载
    $(document).ready(function(){
        /* $("#treeOrg").tree({
            url:'${pageContext.request.contextPath}/org/findCmsOrgTree.do',
            onBeforeLoad : function(node,param){
                if(node!=null){
                    param.parentOrgCode=node.id;
                }else{
                    param.baseOrgCode=parent.currentUser.cmsBaseOrgCode;
                }
            },
            onClick : function(node){
                $('#dgUser').datagrid("load", {
                       'baseOrgCode' : node.id
                   });
            }
        }) */
        //加载树
        admOrgTree("treeOrg","txtOrgName",{onClick: function(node) {
            $('#dgUser').datagrid("load", {
                'orgId' : node.id
            });
        }});

        //加载表格
        var columns = [[{field:'empCode',title:'员工编号',width:120,sortable:true,formatter: formatTip} ,
            {field:'empName',title:'姓名',width:100,formatter: formatTip},
            {field:'orgName',title:'所属机构',width:100,formatter: formatTip},
            /* {field:'mobile',title:'手机号',width:140,formatter: formatTip},
            {field:'email',title:'邮箱',width:140,formatter: formatTip} , */
            {field:'enabled',title:'是否启用',width:100, formatter: formEnabledFlag},
            {field:'createTime',title:'创建时间',align: 'center',width:140, formatter : function(value){
                return formatData(value)
            }},
            {field:'lastLoginTime',title:'最后登录时间',align: 'center',width:140, formatter : function(value){
                return formatData(value)
            }} ]];

        var dataGridParams = {
            url : '${pageContext.request.contextPath}/user/findEmpUser.do',
            pageSize : 10,
            toolbar : '#tlbUser',
            singleSelect : 'true',
            queryParams : {baseOrgCode:portal_global_currentUser.cmsBaseOrgCode},
            fitColumns : 'true',
            onSelect : function(index,row){
                $("#roleList").datalist('loadData',{total:0,rows:[]});
                $("#roleList").datalist({
                    url:'${pageContext.request.contextPath}/user/findAssignRole.do',
                    queryParams:{empCode:row.empCode},
                    textField:'roleName',
                    valueField:'roleCode'
                })
            }
        }
        newloadGrid('dgUser', columns, dataGridParams);

        //加载是否启用数据字典
        uceDictCombobox("cmbEnabled","ENABLE");

        loadDialogDatagrid();

    });
    //初始化是否启用数据字典
    initDictDatas("ENABLE,ORG_FLAG,SUPER_ADMIN");
    //机构标识
    var orgFlag = DictDatas["ORG_FLAG"] && DictDatas["ORG_FLAG"].length > 0 ? DictDatas["ORG_FLAG"][0].typeCode : 'CMS';
    //zTree
    var zTree = new Object();
    //初始化ZTreeNodes
    initUserOrgZTreeNodes({orgFlag:orgFlag},zTree);
    //查询
    function findUser() {
        var queryParams = serializeFormObj("formFindUser");
        var selectNode = $("#treeOrg").tree("getSelected");
        if(selectNode == null){
            queryParams.baseOrgCode=portal_global_currentUser.cmsBaseOrgCode;
        }else{
            queryParams.baseOrgCode=selectNode.id;
        }
        $('#dgUser').datagrid("load", queryParams);
    }

    //重置
    function resetQuery(){
        $("#formFindUser").form('reset');
    }

    //分配
    function openUserOrgDlg() {
        var row = $('#dgUser').datagrid('getSelected');
        if (row==null) {
            showInfoMsg("请选择记录.");
        } else {
            var flag = false;
            DictDatas['SUPER_ADMIN'].map(function(e){
                if(e.typeCode == portal_global_currentUser.empCode){
                    flag=true;
                }
            })
            if(!flag && parseInt(row.orgType) < parseInt(portal_global_currentUser.cmsOrgType)){
                showInfoMsg("普通用户禁止给上级机构用户维护数据权限.");
                return;
            }
            //初始化orgTree树
            $("#orgTreeText").textbox('clear');
            userOrgComboZTree("orgTree",{orgFlag:orgFlag,empCode:row.empCode},{zTreeNodes:zTree.nodes,chkboxType:{"Y": "s", "N": "s" }});
            $('#formRole').form('load', row);
            uceAjax('findOrgCodeByEmpCode.do', {
                empCode : row.empCode
            }, function(data) {
                var ids = data.data;
                if(ids && ids.length > 0 ){
                    userOrgZTreeSetValues('orgTree',ids);
                }
            });
            $('#inpEmpCode').textbox('readonly');
            $('#inpEmpName').textbox('readonly');
            openDialog('dlgUserOrg', '数据权限');
        }
    }

    function loadDialogDatagrid(){
        var roleColumn1 = [[{field:'roleCode',title:'角色编号',align: 'center',width:40},
            {field:'roleName',title:'角色名称',align: 'center',width:60}]];
        var dataGridParm1 = {
            url:'',
            fitColumns : 'true',
            queryParams:{
                empCode : '',
                cmsBaseOrgCode : ''
            },
            toolbar : '#tlbUserRole',
            pagination: false,
            rownumbers: false
        }
        var roleColumn2 = [[{field:'roleCode',title:'角色编号',align: 'center',width:40},
            {field:'roleName',title:'角色名称',align: 'center',width:60}]];
        var dataGridParm2 = {
            url:'',
            fitColumns : 'true',
            queryParams:{
                empCode:''
            },
            toolbar : '',
            pagination: false,
            rownumbers: false

        }
        newloadGrid('dgNotAddedUserRole', roleColumn1, dataGridParm1);
        newloadGrid('dgAddedUserRole', roleColumn2, dataGridParm2);
        $("#dgNotAddedUserRole").datagrid('enableFilter');
    }

    //分配
    function openUserRoleRelDlg() {
        var row = $('#dgUser').datagrid('getSelected');
        if (row==null) {
            showInfoMsg("请选择记录.");
        } else {
            var flag = false;
            DictDatas['SUPER_ADMIN'].map(function(e){
                if(e.typeCode == portal_global_currentUser.empCode){
                    flag=true;
                }
            })
            if(!flag && parseInt(row.orgType) < parseInt(portal_global_currentUser.cmsOrgType)){
                showInfoMsg("普通用户禁止给上级机构用户分配角色.");
                return;
            }
            $("#txtOrgCode").uceCombobox("setValue","");
            $('#txtEmpCode').textbox("setValue", row.empCode);
            $('#txtEmpName').textbox("setValue", row.empName);
            $("#dgNotAddedUserRole").datagrid('loadData',{total:0,rows:[]});
            $("#dgAddedUserRole").datagrid('loadData',{total:0,rows:[]});
            $("#dgAddedUserRole").datagrid('options').url='${pageContext.request.contextPath}/user/findAssignRole.do';
            $("#dgAddedUserRole").datagrid('load',{empCode:row.empCode});

            $("#txtOrgCode").uceCombobox({
                url:'${pageContext.request.contextPath}/user/findUserBindOrg.do',
                valueField:'orgCode',
                textField:'orgName',
                queryParams : {empId:row.empId,empCode:row.empCode},
                limitToList : true,
                editable : false,
                onLoadSuccess: function () {  //加载完成后,设置选中第一项
                    var val = $(this).combobox("getData");
                    for (var item in val[0]) {
                        if (item == "orgCode") {
                            $(this).combobox("select", val[0][item]);
                        }
                    }
                },
                onChange : function (newValue,oldValue){
                    $("#dgNotAddedUserRole").datagrid('loadData',{total:0,rows:[]});
                    if(newValue != null && newValue != ""){
                        var url='${pageContext.request.contextPath}/user/findUnassignRole.do';
                        var roleCodeList = $('#dgAddedUserRole').datagrid('getRows');
                        roleCodeList.map(function(item,index){
                            var regx = /\?/;
                            regx.test(url) ? url=url+"&roleCodeList="+item.roleCode : url = url+"?roleCodeList="+item.roleCode;

                        })
                        $("#dgNotAddedUserRole").datagrid('options').url=url;
                        $("#dgNotAddedUserRole").datagrid('load',{empCode:row.empCode,cmsBaseOrgCode:newValue});
                    }
                }
            })
            openDialog('dlgUserRoleRel', '角色设置');
        }
    }

    //给用户移入角色
    function moveUserRoleIn(){
        var checkRows = $('#dgNotAddedUserRole').datagrid("getSelections");
        if(checkRows != null && checkRows.length > 0){
            for(var i = 0;i < checkRows.length; i++){
                //添加到角色到已添加这边
                $('#dgAddedUserRole').datagrid('appendRow',checkRows[i]);
                //删除未添加这边的该角色
                var rowIndex = $('#dgNotAddedUserRole').datagrid('getRowIndex',checkRows[i])
                $('#dgNotAddedUserRole').datagrid('deleteRow',rowIndex);
            }
        }
    }

    //给用户移入全部角色
    function moveUserRoleAllIn(){
        var checkRows = $('#dgNotAddedUserRole').datagrid("getRows");
        if(checkRows != null && checkRows.length > 0){
            var rowNum = checkRows.length;
            for(var i = 0;i <rowNum;  i++){
                //添加到角色到已添加这边
                $('#dgAddedUserRole').datagrid('appendRow',checkRows[0]);
                //删除未添加这边的该角色
                var rowIndex = $('#dgNotAddedUserRole').datagrid('getRowIndex',checkRows[0])
                $('#dgNotAddedUserRole').datagrid('deleteRow',rowIndex);
            }
        }
    }

    //给用户移除角色
    function moveUserRoleOut(){
        var checkRows = $('#dgAddedUserRole').datagrid("getSelections");
        if(checkRows != null && checkRows.length > 0){
            for(var i = 0;i < checkRows.length; i++){
                //把角色移回到未添加这边
                $('#dgNotAddedUserRole').datagrid('appendRow',checkRows[i]);
                //删除未添加这边的该角色
                var rowIndex = $('#dgAddedUserRole').datagrid('getRowIndex',checkRows[i])
                $('#dgAddedUserRole').datagrid('deleteRow',rowIndex);
            }
        }
    }

    //给用户移除角色
    function moveUserRoleAllOut(){
        var checkRows = $('#dgAddedUserRole').datagrid("getRows");
        if(checkRows != null && checkRows.length > 0){
            var rowNum = checkRows.length;
            for(var i = 0;i < rowNum; i++){
                //把角色移回到未添加这边
                $('#dgNotAddedUserRole').datagrid('appendRow',checkRows[0]);
                //删除未添加这边的该角色
                var rowIndex = $('#dgAddedUserRole').datagrid('getRowIndex',checkRows[0])
                $('#dgAddedUserRole').datagrid('deleteRow',rowIndex);
            }
        }
    }

    // 保存用户角色
    function saveUserRole(btn) {
        $(btn).linkbutton('disable');
        var roleRows = $('#dgAddedUserRole').datagrid('getData').rows;
        var roleCodeListStr = '';
        if (roleRows && roleRows.length > 0) {
            for(var i=0;i<roleRows.length;i++){
                roleCodeListStr = roleCodeListStr + roleRows[i].roleCode + ',';
            }
        }
        $.ajax({
            url:'${pageContext.request.contextPath}/user/saveUserRoleRel.do',
            data :{'empCode': $("#txtEmpCode").val(),'roleCodeListStr':roleCodeListStr},
            task : function(data, statusText, xhr){
                $("#roleList").datalist('loadData',{total:0,rows:[]});
                $("#roleList").datalist('reload');
                $('#dlgUserRoleRel').dialog('close');
            },
            finalTask:function(){
                $(btn).linkbutton('enable');
            }
        });
    }

    //保存用户机构
    function saveUserOrg(btn) {
        $(btn).linkbutton('disable');
        $("#formRole").form('submit',{
            url : '${pageContext.request.contextPath}/user/updateUserOrg.do',
            task: function(result){
                $(btn).linkbutton('enable');
                closeDialog('dlgUserOrg');
                reloadDatagrid('dgUser');
            }
        });
    }



</script>
<!--左侧树-->
<div data-options="region:'west',border:false" style="width:200px;">
    <div class="easyui-layout" data-options="fit:true,border:false">
        <div data-options="region:'north',border:false" >
            <div style="margin-bottom:0px" data-options="region:'north',border:false" >
                <!--  <input class="easyui-searchbox" id="txtOrgName" style="width:100%;" > -->
            </div>
        </div>
        <div data-options="region:'center'">
            <ul id="treeOrg"></ul>
        </div>
    </div>
</div>
<!--列表-->
<div data-options="region:'center',border:false" style="width:600px;">
    <div class="easyui-layout" data-options="fit:true,border:false">
        <div data-options="region:'center',border:true" style="width:230px;height:100px;border:5px;">
            <div id="tlbUser" style="padding:5px;height:auto">
                <form action="#" id="formFindUser">
                    <div id="aa" class="easyui-accordion">
                        <div title="" data-options="selected:true">
                            <ul class="search-form" >
                                <li><input class="easyui-textbox" label="员工编号：" name="empCode" data-options="prompt:' 请输入员工编号'"></li>
                                <li><input class="easyui-textbox" label="员工名称："  name="empName" data-options="prompt:' 请输入员工名称'"></li>
                                <li><select name="enabled" label="是否启用："  style="width:100px;" id="cmbEnabled"></select> </li>
                                <div>
                                    <a href="#" class="easyui-linkbutton search" onclick="findUser()">查询</a>
                                    <a href="#" class="easyui-linkbutton reset" onclick="resetQuery()">重置</a>
                                </div>

                            </ul>
                        </div>
                    </div>
                    <div class="toolbar-margin">
             <%--           <shiro:hasPermission name="user_allocate"><a href="javascript:void(0)" class="easyui-linkbutton" iconCls="iconfont uce-status" onclick="openUserRoleRelDlg()" plain="true">分配角色</a></shiro:hasPermission>
                        <shiro:hasPermission name="user_org"><a href="javascript:void(0)" class="easyui-linkbutton" iconCls="iconfont uce-permission" onclick="openUserOrgDlg()" plain="true">数据权限</a></shiro:hasPermission>--%>
                 <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="iconfont uce-status"  onclick="userConfig()" plain="true">话务配置</a>
                    </div>
                </form>
            </div>
            <input id="dgUser" class="easyui-datagrid" data-options="fit:true">
        </div>
        <div data-options="region:'east'" style="width:200px;">
            <ul class="easyui-datalist" id="roleList" data-options="border:false"></ul>
        </div>
    </div>
</div>
<!--编辑-->
<div id="dlgUserRoleRel" class="easyui-dialog"  data-options="title:'角色设置',closed:true" style="padding:5px 5px;background:#fff;height:600px;width:850px" buttons="#dlgUserRoleRel-btn">
    <div class="easyui-layout"  data-options="fit:true">
        <div data-options="region:'north'" style="height:60px; width:750px; padding:10px;">
            所属机构: <input class="easyui-combobox"  id="txtOrgCode" style="width:200px" >
            员工编号: <input class="easyui-textbox"  id="txtEmpCode" disabled="disabled">
            员工名称: <input class="easyui-textbox" id="txtEmpName" disabled="disabled">
        </div>

        <div data-options="region:'east',title:'已添加角色',collapsible:false" style="width:360px; height:auto">
            <table id="dgAddedUserRole"  class="easyui-datagrid" style="width:100%;border:false" ></table>
        </div>
        <div data-options="region:'west',title:'可添加角色',collapsible:false" style="width:360px; height:auto">
            <table id="dgNotAddedUserRole" class="easyui-datagrid" style="width:100%;;border:false" ></table>
        </div>

        <div data-options="region:'center',border:false" style="padding-top:120px; padding-left:5px; height:200px" >
            <a href="javascript:void(0)" class="easyui-linkbutton c1" style="width:110px; margin-bottom:10px;" onclick="moveUserRoleAllIn()"> 全部移入》</a>
            <a href="javascript:void(0)" class="easyui-linkbutton c1" style="width:110px; margin-bottom:10px;" onclick="moveUserRoleIn()"> 移入》</a>

            <a href="javascript:void(0)" class="easyui-linkbutton c5" style="width:110px; margin-bottom:10px;" onclick="moveUserRoleOut()">《移出</a>
            <a href="javascript:void(0)" class="easyui-linkbutton c5" style="width:110px; margin-bottom:10px;" onclick="moveUserRoleAllOut()">《全部移出</a>
        </div>
    </div>

</div>
<div id="dlgUserRoleRel-btn" style="padding-top:20px; padding-left:600px; border:2px;">
    <a href="javascript:void(0)" class="easyui-linkbutton save"  onclick="saveUserRole(this)">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton call" onclick="closeDialog('dlgUserRoleRel')">取消</a>
</div>

<!--用户机构dialog-->
<div id="dlgUserOrg" class="easyui-dialog"  data-options="title:'角色设置',closed:true" style="background:#fff;height:390px;width:430px;" buttons="#dlgUserOrg-btn">
    <form id="formRole" method="post">
        <div style="padding: 15px 20px; background: #fff;">
            <table class="table" style="width: 100%; border: 0px;">
                <tbody>
                <tr>
                    <td style="width: 15%;">员工编号:</td>
                    <td><input name="empCode" id="inpEmpCode" class="easyui-textbox"  style="width: 300px;height: 40px"></input></td>
                </tr>
                <tr>
                    <td style="width: 15%;">员工姓名:</td>
                    <td><input name="empName" id="inpEmpName" class="easyui-textbox"  style="width: 300px;height: 40px"></input></td>
                </tr>
                <tr>
                    <td>机构:</td>
                    <td><div id="orgTreeBox">
                        <input id="orgTreeText" class="easyui-textbox" style="width:300px;height:40px" >
                        <div id="orgTreePanel" class="ztree-panel" style="height:200px;width:298px">
                            <ul id="orgTree" class="ztree"></ul>
                        </div>
                    </div>
                    </td>
                </tr>
                <tr>
                    <td>备注:</td>
                    <td><input name="remark" class="easyui-textbox" validtype="length[1,125]" multiline="true"
                               style="height: 120px; width: 300px"></td>
                </tr>
                </tbody>
            </table>
        </div>
    </form>
</div>

<div id="dlUserConfig" class="easyui-dialog" data-options="closable: false"
     style="width: 450px; height: 450px; padding: 5px" closed="true" buttons="#saveCustomerInformation">
    <div class="easyui-layout" data-options="fit:true">
        <div data-options="region:'center',border:false"
             style="padding: 15px 20px; background: #fff;">
            <form id="userConfigFromId" method="post">
                <input type="hidden" name="isExistence" id="isExistence">
                <table class="table" style="width: 400px; border: 0px;">
                    <tbody>
                    <tr style="padding-left: 20px">
                        <td width="80px">员工:</td>
                        <td width="300px"><input name="agentName" id="agentName"
                                                 class="easyui-textbox" style="width:300px;height: 40px" readonly="true"></td>
                    </tr>
                    <tr style="padding-left: 20px">
                        <td>user_code:</td>
                        <td><input name="userId" id="userId" class="easyui-textbox"
                                   style="width:300px;height: 40px" readonly="true">
                        </td>
                    </tr>
                    <tr style="padding-left: 20px">
                        <td>cti_openid:</td>
                        <td><input name="ctiOpenid" id="ctiOpenid" class="easyui-textbox"
                                   style="width:300px;height: 40px" data-options="required:true" >
                        </td>
                    </tr>
                    <tr style="padding-left: 20px">
                        <td>cti密码:</td>
                        <td><input  name="ctiPwd" id="ctiPwd" class="easyui-textbox"
                                    style="width:300px;height: 40px"  >
                        </td>
                    </tr>
                    <tr>
                        <td>备注:</td>
                        <td><input name="remark" id="remark" class="easyui-textbox"
                                   validtype="length[1,125]" multiline="true"
                                   style="height: 120px; width:300px"></td>
                    </tr>
                    </tbody>
                </table>
            </form>
        </div>
    </div>
</div>

<!--新建客户信息开始-->
<%--<div id="dlUserConfig"  class="easyui-dialog"
     style="width: 700px; height: 400px; padding: 5px;display:none" closed="true" data-options="closable: false" buttons="#saveCustomerInformation">
    <form name="" id="userConfigFromId">
        <div class="acc-row ">
          <div class="acc-items width50">
              <input type="hidden" name="isExistence" id="isExistence">
              <div class="acc-title" style="padding-top:9px">员工<span style="color: red;"></span>：</div>
              <div class="acc-con">
                  <div class="acc-con-main">
                      <input name="agentName" style="width:220px;height: 28px" id="agentName"
                             class="easyui-textbox" readonly  />
                  </div>
              </div>
          </div>
            <div class="acc-items width50">
                <div class="acc-title" style="padding-top:9px">cti_openid<span style="color: red;">*</span>：</div>
                <div class="acc-con">
                    <div class="acc-con-main">
                        <input name="ctiOpenid" style="width:220px;height: 28px" id="ctiOpenid"
                               class="easyui-textbox"  data-options="required:true"/>
                    </div>
                </div>
            </div>
            <div class="acc-items width50">
                <div class="acc-title" style="padding-top:9px">user_code<span style="color: red;"></span>：</div>
                <div class="acc-con">
                    <div class="acc-con-main">
                        <input name="userId" style="width:220px;height: 28px" id="userId"
                               class="easyui-textbox" readonly/>
                    </div>
                </div>
            </div>
            <div class="acc-items width50">
                <div class="acc-title" style="padding-top:9px">cti密码<span style="color: red;"></span>：</div>
                <div class="acc-con">
                    <div class="acc-con-main">
                        <input name="ctiPwd" style="width:220px;height: 28px" id="ctiPwd"
                               class="easyui-textbox" />
                    </div>
                </div>
            </div>
            <div class="acc-items width100">
                <div class="acc-title" style="padding-top:9px">备注信息<span style="color: red;"></span>：</div>
                <div class="acc-con">
                    <div class="acc-con-main">
                        <input name="remark" style="width:220px;height: 28px" id="remark"
                               class="easyui-textbox" />
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>--%>
<div id="saveCustomerInformation">
    <a href="javascript:void(0)" class="easyui-linkbutton  save" tag="" id="saveCsCustomerInformation" onclick="saveConfig()" >保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton call" tag="" id="cancleCsCustomerInformation" onclick="cancleConfig()">取消</a>
</div>
<div id="dlgUserOrg-btn" style=" margin-bottom:10px;margin-top:10px;border:2px;">
    <a href="javascript:void(0)" class="easyui-linkbutton save"  onclick="saveUserOrg(this)">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton call" onclick="closeDialog('dlgUserOrg')">取消</a>
</div>

</body>
</html>